package com.itheima.sh.order.service;

import com.itheima.sh.order.mapper.OrderMapper;
import com.itheima.sh.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
public class OrderService {

    @Autowired
    OrderMapper orderMapper;


    public List<Order> findOrderByUsername(String username) {

        // 参数校验
        if (username == null) {
            throw new RuntimeException("参数非法");
        }

        Example example = new Example(Order.class);
        // 条件查询 mybatis 动态条件查询   动态SQL
        // where username = ''
        Example.Criteria criteria = example.createCriteria();
        // 参数1：实体类中属性名称
//        if (username != null && username.length() >0) {
//            criteria.andEqualTo("username", username);
//        }
        criteria.andEqualTo("username", username);

//        int i = 1 / 0;

        List<Order> orderList = orderMapper.selectByExample(example);

        if (orderList == null) {
            return new ArrayList<>();
        }

        return orderList;

    }


}
